package com.ilop.sthome.ui.adapter.scene;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-03-16.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class TriggerOutAdapter extends RecyclerView.Adapter<TriggerOutAdapter.ItemHolder> {

    private Context mContext;
    private List<DeviceInfoBean> mList;
    private boolean isEditable = true;

    public TriggerOutAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<DeviceInfoBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setEditable(boolean editable) {
        this.isEditable = editable;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_trigger, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        DeviceInfoBean device = mList.get(i);
        itemHolder.mName.setTextColor(isEditable ? ContextCompat.getColor(mContext, R.color.text_title) : ContextCompat.getColor(mContext, R.color.text_normal));
        itemHolder.mAction.setVisibility(View.VISIBLE);
        itemHolder.mImg.setImageResource(SmartDevice.getType(device.getDevice_type()).getDrawableResId());

        if ("GATEWAY".equals(device.getDevice_type())){
            DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(device.getDeviceName());
            itemHolder.mName.setText(gateway.getNickName()==null ? mContext.getString(R.string.ali_gateway) : gateway.getNickName());
            itemHolder.mAction.setText(CoderUtils.analysisGatewayActionCode(mContext, device.getDeviceName(), device.getDevice_status()));
        }else if ("PHONE".equals(device.getDevice_type())){
            itemHolder.mName.setText(mContext.getString(R.string.phone));
            itemHolder.mAction.setText(mContext.getString(R.string.send_mobile_push_when_conditions_are_met));
        } else {
            DeviceInfoBean subDevice = DeviceDaoUtil.getInstance().findByDeviceId(device.getDeviceName(), device.getDevice_ID());
            if (subDevice == null){
                String name = mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId());
                itemHolder.mName.setText(name);
                itemHolder.mAction.setText(mContext.getString(R.string.lack_of_equipment));
            }else {
                if (TextUtils.isEmpty(subDevice.getSubdeviceName())) {
                    String name = mContext.getString(SmartDevice.getType(subDevice.getDevice_type()).getTypeStrId()) + subDevice.getDevice_ID();
                    itemHolder.mName.setText(name);
                } else {
                    itemHolder.mName.setText(subDevice.getSubdeviceName());
                }
                if(SmartDevice.getType(device.getDevice_type()) == SmartDevice.EE_TEMP_CONTROL){
                    itemHolder.mAction.setText(CoderUtils.getThermostatMsg(mContext, device.getDevice_status()));
                }else {
                    itemHolder.mAction.setText(CoderUtils.getDeviceTypeMsg(mContext, device.getDevice_type(), device.getDevice_status()));
                }
            }
        }

        if (!TextUtils.isEmpty(device.getDevice_status())) {
            int codeLength = device.getDevice_status().length();
            if (codeLength > 4 && !"PHONE".equals(device.getDevice_type()) && !"82".equals(device.getDevice_status().substring(0, 2))) {
                String time = device.getDevice_status().substring(codeLength - 4);
                String delay = CoderUtils.getDelayTime(mContext, time);
                String delayText = mContext.getString(R.string.delay) + delay;
                itemHolder.mDelay.setVisibility(View.VISIBLE);
                itemHolder.mDelay.setText(delayText);
            } else {
                itemHolder.mDelay.setVisibility(View.GONE);
                itemHolder.mDelay.setText(null);
            }
        }else {
            itemHolder.mDelay.setVisibility(View.GONE);
            itemHolder.mDelay.setText(null);
        }

        itemHolder.itemView.setEnabled(isEditable);
        itemHolder.itemView.setOnClickListener(view -> LiveDataBus.get().with(CommonId.OUTPUT_CLICK).setValue(i));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_device_delay)
        TextView mDelay;
        @BindView(R.id.item_trigger_img)
        ImageView mImg;
        @BindView(R.id.item_trigger_name)
        TextView mName;
        @BindView(R.id.item_trigger_action)
        TextView mAction;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

